<?php

require_once("page.class.php");
require_once("db_functions.php");

define("PAGE_LENGTH",30);

class brands extends page{
	
	var $db;
	
	function brands(){
		$this->db = new db_functions();
		
		
		$action = getFromInput("a",true);
		$mod = getFromInput("mod",true);
		
		switch ($action){
			case "search":
				switch ($mod){
					case "komitent":
						$this->searchKomitent();
						break;
				}
				break;
			case "new":
				$this->novi();
				break;
			case "save":
				$this->save();
				break;
			case "delete":
				$this->delete();
				break;
			default:
				$this->main();
				break;
		}
	}
	
	//---------------------------------------------------------------------------------
	
	function main(){
		$kword = readValFromInput("kword",true);
		$page = getFromInput("page",true);
		$page = $page>0?$page:1;
		
		
		$this->assign("kword",$kword);
		
		$start = ($page-1)*PAGE_LENGTH;
		
		$q = "SELECT * FROM brands WHERE brand_id>0 ";
		if (strlen($kword)>0) {
			$q .= "AND (brand_id='".prepForSQL($kword)."' OR brand_name LIKE '%".prepForSQL($kword)."%') ";
		}
		$ukupno = count($this->db->select1($q)); $this->assign("info","Ukupno: $ukupno");
		$q .= "ORDER BY brand_name, brand_id LIMIT $start,".PAGE_LENGTH;
		$res = $this->db->select2($q);
		if ($res !== false) {
			$numpages = bcdiv($ukupno,PAGE_LENGTH,0);
			if ($ukupno%PAGE_LENGTH) {
				$numpages++;
			}
			$pages = PageFilter($page,$numpages);
			$this->assign("page",$pages["page"]);
			$this->assign("page_current",$pages["current"]);
			
			foreach ($res as $k=>$v){
				$brand_ids[$k] = $v["brand_id"];
				$brand_names[$k] = htmlentities(trim($v["brand_name"]),null,"UTF-8");
				$brand_names_enc[$k] = str_replace('"',"&quot;",prepForSQL(trim($v["brand_name"])));
			}
			$this->assign("brand_id",$brand_ids);
			$this->assign("brand_name",$brand_names);
			$this->assign("brand_name_enc",$brand_names_enc);
		}else{
			$this->assign("comment_start","<!--");
			$this->assign("comment_end","-->Nema rezultata");
		}
		
		echo $this->parseTemplate("brands/main.tpl");
	}
	
	//---------------------------------------------------------------------------------
	
	function searchKomitent(){
		$ul = getFromInput("ul");
		$kword = readValFromInput("keyword");
		
		$num_found = "0";
		$this->assign("keyword",$kword);
		
		$q = "SELECT brand_name, brand_id FROM brands WHERE brand_id>0 AND brand_name LIKE '%$kword%' GROUP BY brand_name ORDER BY brand_name, brand_id";
		$res = $this->db->select2($q);
		if ($res !== false) {
			$num_found = count($res);
			foreach ($res as $k=>$v){
				$brand_ids[$k] = $v["brand_id"];
				$items[$k] = $v["brand_name"];
				$links[$k] = "AddKomitentBrand('$ul','".$v["brand_id"]."','".$v["brand_name"]."')";
			}
			$this->assign("item_id",$brand_ids);
			$this->assign("item",$items);
			$this->assign("link",$links);
		}
		$this->assign("num_found",$num_found);
		echo $this->parseTemplate("brands/search.tpl");
	}
	
	//---------------------------------------------------------------------------------
	
	function novi(){
		$ime = readValFromInput("name");
		$code = 0;
		$msg = "Brend je dodat.";
		
		if (!$this->db->alterTable("INSERT INTO brands(brand_name) VALUES('".addslashes($ime)."')")) {
			$code = 1;
			$msg = "Unos nije izvršen!\n".mysql_error();
		}
		
		echo "$code{!}$msg";
	}
	
	function save(){
		$id = getFromInput("edit_brand_id");
		$name = readValFromInput("edit_brand_name");
		
		if ($id>0) {
			$q = "UPDATE `brands` SET `brand_name`='".addslashes($name)."' WHERE `brand_id`=$id LIMIT 1";
		}else{
			$q = "INSERT INTO brands(brand_name) VALUES('".addslashes($name)."')";
		}
		if($this->db->alterTable($q)){
			echo "0{!}Brend je sačuvan.";
		}else{
			echo "1{!}Brend nije sačuvan.";
		}
	}
	
	//---------------------------------------------------------------------------------
	
	function delete(){
		$id = getFromInput("id");
		$code = 0;
		$msg = "Brend je obrisan.";
		$q = "DELETE FROM brands WHERE brand_id=$id LIMIT 1";
		if(!$this->db->alterTable($q)){
			$code=1;
			$msg = "Brisanje nije uspelo!\n".mysql_error();
		}
		echo "$code{!}$msg";
	}
}

?>